
# 使用生成器创建新的迭代模式，想实现一个自定义迭代模式
def frange(start, stop, increment):
    x = start
    while x < stop:
        yield x
        x += increment

# 函数中存在yield可将其转换为一个生成器，生成器只能用于迭代操作
def countdown(n):
    print('starting to count from ', n)
    while n > 0:
        yield n
        n -= 1
    print('Done !')

def demo_1():
    for n in frange(0, 4, 0.5):
        print(n)
    print(list(frange(0, 1, 0.125)))

def demo_2():
    c = countdown(3)
    print(c)
    print(next(c))
    print(next(c))
    print(next(c))
    print(next(c))

if __name__ == '__main__':
    # demo_1()
    demo_2()